import { Link } from 'react-router-dom';
import React from 'react';
import styles from './index.less';
import Counter from '@/components/Counter';
import { iframePostMessageFnify } from 'hzero-front/lib/utils/iframe';

const openTab = iframePostMessageFnify({
  fn: 'openTab'
});
const getUserMessage = iframePostMessageFnify({
  fn: 'getUserMessage'
});

const Page = () => {

  const [parentUserInfo, setParentUserInfo] = React.useState<any>({});

  const handleClickOpenParentPage = async () => {
    if (self !== top && top) {
      // 打开用户主题页面
      await openTab({
        key: '/workplace'
      });
    }
  };

  const handleClickGetSelfData = async () => {
    if (self !== top && top) {
      // 打开用户主题页面
      const ms = await getUserMessage();
      console.log(ms);
      setParentUserInfo(ms);
    }
  };

  return (
    <div>
      <Counter />
      <h1 className={styles.title}> Sub app Page index home1</h1>
      <div>
        <Link style={{ color: 'red', background: '#fff', marginRight: 10 }} to="/">/back</Link>
      </div>
      <button onClick={handleClickOpenParentPage}>测试打开工作台</button>
      <button onClick={handleClickGetSelfData}>测试获取父应用数据</button>
      <div>
        <textarea value={JSON.stringify(parentUserInfo, null, 2)} rows={50} cols={200} />
      </div>
    </div>
  );
};

export default Page;